package com.budwk.app.waterquality.controllers.admin;

import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.models.FormsTemplateConfig;
import com.budwk.app.waterquality.services.FormsTemplateConfigService;
import com.budwk.app.waterquality.vo.TemplateConfigQueryVo;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import javax.servlet.http.HttpServletResponse;

@IocBean
@At("/waterquality/admin/forms/template/config")
@ApiDefinition(tag = "报表管理模板配置类")
@SLog(tag = "报表管理模板配置类")
@Slf4j
public class FormsTemplateConfigController {
    @Inject
    private FormsTemplateConfigService templateConfigService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询报表管理模板配置类")
    public Result<?> listByPage(@Param("..") TemplateConfigQueryVo bean) {
        return Result.data(templateConfigService.listByPage(bean));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询所有的模板信息")
    public Result<?> listAll() {
        return Result.data(templateConfigService.query());
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "新增报表管理模板配置类")
    @ApiOperation(name = "新增报表管理模板配置类")
    @ApiResponses
    public Result<?> create(@Param("..")FormsTemplateConfig c) {
        templateConfigService.create(c);
        return Result.success();
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取报表管理模板配置类")
    public Result<?> getData(@Param("id") String id) {
        FormsTemplateConfig wkFormsTemplateConfig = templateConfigService.fetch(id);
        return Result.data(wkFormsTemplateConfig);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "下载报表数据")
    public Result<?> downTemplateData(@Param("templateId") String templateId, HttpServletResponse response) {
        try {
            return Result.data(templateConfigService.downTemplateData(templateId, response));
        } catch (Exception e) {
            log.error("下载报表数据 error: ", e);
            throw new AppException("下载报表数据，请联系管理员！");
        }
    }

}
